package com.hz.filemanager.ui.file.adapter;

import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hz.filemanager.R;
import com.hz.filemanager.model.file.FileBean;
import com.hz.filemanager.model.file.FileManager;
import com.hz.filemanager.model.file.MusicFolder;
import com.hz.filemanager.utils.Utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ApkAdapter extends RecyclerView.Adapter<ApkAdapter.ViewHolder>{

    private List<FileBean> datas = new ArrayList<>();
    private List<FileBean> selectApks = new ArrayList<>();
    private OnLongClickListener onLongClickListener;
    private OnClickListener onClickListener;
    private boolean isSelectModel = false;

    public List<FileBean> getSelectApks() {
        return selectApks;
    }

    public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
        this.onLongClickListener = onLongClickListener;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public boolean isSelectModel() {
        return isSelectModel;
    }

    public void setSelectModel(boolean selectModel) {
        isSelectModel = selectModel;
    }

    public void setDatas(List<FileBean> datas) {
        if(datas == null || datas.isEmpty()){
            this.datas.clear();
        }else{
            this.datas.clear();
            this.datas.addAll(datas);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_apk, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            FileBean fileBean = datas.get(position);
            holder.titleTv.setText(fileBean.getFileName());
            Drawable apkIcon = FileManager.getApkIcon(Utils.getContext(), fileBean.getFilePath());
            if(apkIcon == null){
                holder.iconIv.setImageResource(fileBean.getIconId());
            }else{
                holder.iconIv.setImageDrawable(apkIcon);
            }

        if(isSelectModel){
            holder.cb.setVisibility(View.VISIBLE);
            holder.cb.setChecked(selectApks.contains(fileBean));
        }else{
            holder.cb.setVisibility(View.INVISIBLE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(isSelectModel){
                    if(selectApks.contains(fileBean)){
                        selectApks.remove(fileBean);
                        holder.cb.setChecked(false);
                    }else{
                        selectApks.add(fileBean);
                        holder.cb.setChecked(true);
                    }
                }
                if(onClickListener != null){
                    onClickListener.onClick(v, fileBean);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                if(onLongClickListener != null && !isSelectModel){
                    onLongClickListener.onLongClick();
                }
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void clear() {
        this.datas.clear();
        notifyDataSetChanged();
    }

    public void checkAll(boolean isCheck) {

        selectApks.clear();
        if (isCheck){
            selectApks.addAll(datas);
        }
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{

        private final TextView titleTv;
        private final TextView sizeTv;
        private final TextView dateTv;
        private final CheckBox cb;
        private final ImageView iconIv;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iconIv = itemView.findViewById(R.id.icon_iv);
            titleTv = itemView.findViewById(R.id.title_tv);
            sizeTv = itemView.findViewById(R.id.size_tv);
            dateTv = itemView.findViewById(R.id.date_tv);
            cb = itemView.findViewById(R.id.cb);
        }
    }

    public static interface OnLongClickListener{
        void onLongClick();
    }
    public static interface OnClickListener{
        void onClick(View view, FileBean fileBean);
    }
}
